let redis = require("../util/redisDB")
const util = require('../util/common')
const crypto = require('crypto');
// 获得用户资料，不能包含密码
exports.getUserInfo = (req, res, next) => {

//    获得用户资料存在两种情况，一种是自己的资料，一种是他人资料
    redis.get(req.headers.fapp + ":user:info:" + req.params.username).then((data) => {
        if (data) {
            if (req.params.username == req.username) {
                //    自己的资料
                delete data.password
            } else {
                //    他人资料，通过username查找
                delete data.phone
                delete data.password
            }
            res.json(util.getReturnData(0, '', data))
        } else {
            res.json(util.getReturnData(1, '用户不存在'))
        }
    })
}
// 更改用户资料
exports.changeUserInfo = (req, res, next) => {
    let key = req.headers.fapp + ":user:info:" + req.username
    redis.get(key).then((data) => {
        if (data) {
            let userData = {
                username: req.username,
                phone: 'phone' in req.body ? req.body.phone : data.phone,
                nikename: 'nikename' in req.body ? req.body.nikename : data.nikename,
                age: 'age' in req.body ? req.body.age : data.age,
                sex: 'phone' in req.body ? req.body.sex : data.sex,
                password: 'password' in req.body ? req.body.password : data.password,
                // 用户是否被封停
                login: data.login
            }
            redis.set(key, userData)
            res.json(util.getReturnData(0, '修改成功'))
        } else {
            res.json(util.getReturnData(1, '修改失败'))
        }
    })
}

//获取所有文章分类
exports.getArticleType = (req, res, next) => {
    redis.get("book:a_type").then((data) => {
        res.json(util.getReturnData(0, '', data))
    })
}
//文章点赞或踩
exports.articleLike = (req, res, next) => {
    let member = req.headers.fapp + ":article:" + req.params.id
    let like = req.params.like
    if (like == 0) {
        //采用自减操作进行
        redis.zincrby(req.headers.fapp + ":a_like", member, -1)
    } else {
        //采用自增操作进行
        redis.zincrby(req.headers.fapp + ":a_like", member)
    }
    res.json(util.getReturnData(0, 'success'))
}
//文章收藏
exports.articleCollection = (req, res, next) => {
    let key = req.headers.fapp + ":user:" + req.username + ":collection"
    //获取参数
    let a_key = req.headers.fapp + ":article:" + req.params.id
    redis.get(a_key).then((data) => {
        if(data){
            //获取原本的
            redis.get(key).then((tData) => {
                if (!tData) tData = []
                tData.push({time: Date.now(), a_id: req.params.id, title: data.title})
                redis.set(key, tData)
                res.json(util.getReturnData(0, 'success'))
            })
        }else{
            res.json(util.getReturnData(1, '文章不存在'))
        }
    })
}

//获取收藏地址
exports.getCollection = (req, res, next) => {
    let key = req.headers.fapp + ":user:" + req.username + ":collection"
    redis.get(key).then((data) => {
        res.json(util.getReturnData(0, '', data))
    })
}

//文章评论
exports.articleTalk = (req, res, next) => {
    if ('a_id' in req.body && 'talk' in req.body) {
        //组织字符串
        let talk = {talk: req.body.talk, time: Date.now(), username: req.username}
        let key = req.headers.fapp + ':article:' + req.body.a_id + ':talk'
        redis.get(key).then((data) => {
            let tData = []
            if (data) {
                tData = data
                tData.push(talk)
            } else {
                tData.push(talk)
            }
            redis.set(key, tData)
            res.json(util.getReturnData(0, '评论成功'))
        })
    } else {
        res.json(util.getReturnData(1, '评论错误'))
    }
}